﻿using System;
using System.Collections.Generic;
using System.Windows.Media;
using Ninject;
using SydEnergiPrototype.DataAccess;
using SydEnergiPrototype.Logic;
using SydEnergiPrototype.Model;

namespace SydEnergiPrototype.ViewModel
{
    public class CachedDataViewModel : IDataViewModel
    {
        public string Header
        {
            get
            {
                if (_fragment == "EditData")
                    return "Rediger Data";

                return "Tilføj Data";
            }
        }

        public List<string> CableOptions
        {
            get { return _itsDataViewModel.CableOptions; }
        }

        public List<string> TeletubeOptions
        {
            get { return _itsDataViewModel.TeletubeOptions; }
        }

        public int CableSelectedIndex
        {
            get { return _itsDataViewModel.CableSelectedIndex; }
            set { _itsDataViewModel.CableSelectedIndex = value; }
        }

        public int TeletubeSelectedIndex
        {
            get { return _itsDataViewModel.TeletubeSelectedIndex; }
            set { _itsDataViewModel.TeletubeSelectedIndex = value; }
        }

        public int FiberSelectedIndex
        {
            get { return _itsDataViewModel.FiberSelectedIndex; }
            set { _itsDataViewModel.FiberSelectedIndex = value; }
        }

        public int TubeSelectedIndex
        {
            get { return _itsDataViewModel.TubeSelectedIndex; }
            set { _itsDataViewModel.TubeSelectedIndex = value; }
        }

        private RelayCommand _saveEntryCommand;
        public RelayCommand SaveEntryCommand
        {
            get
            {
                if (_saveEntryCommand == null)
                    return _saveEntryCommand = new RelayCommand(param => Save());

                return _saveEntryCommand;
            }
        }

        public RelayCommand ClearFieldsCommand
        {
            get { return _itsDataViewModel.ClearFieldsCommand; }
        }

        public List<SolidColorBrush> ColorCodes
        {
            get { return _itsDataViewModel.ColorCodes; }
            set { _itsDataViewModel.ColorCodes = value; }
        }

        public IEntry CurrentEntry
        {
            get { return _itsDataViewModel.CurrentEntry; }
            set { _itsDataViewModel.CurrentEntry = value; }
        }

        private readonly IDataViewModel _itsDataViewModel;
        private readonly IMemoryDataStore _memoryDataStore;
        private readonly IFactory _factory;
        private readonly string _fragment;

        [Inject]
        public CachedDataViewModel(IDataViewModel dataViewModel, IMemoryDataStore memoryDataStore, IFactory factory, string fragment)
        {
            if (dataViewModel == null)
                throw new ArgumentNullException("dataViewModel");
            _itsDataViewModel = dataViewModel;

            if (memoryDataStore == null)
                throw new ArgumentNullException("memoryDataStore");
            _memoryDataStore = memoryDataStore;

            if(factory == null)
                throw new ArgumentNullException("factory");
            _factory = factory;

            if (string.IsNullOrWhiteSpace(fragment))
                throw new ArgumentNullException("fragment");
            _fragment = fragment;

            InitDecoratedCurrentEntry();
        }

        private void InitDecoratedCurrentEntry()
        {
            switch (_fragment)
            {
                case "AddData":
                    InitializeEmptyEntryWithCabinetId();
                    break;
                case "EditData":
                    _itsDataViewModel.CurrentEntry = _memoryDataStore.RetrieveEntry();
                    break;
                default:
                    InitializeEmptyEntryWithCabinetId();
                    break;
            }
        }

        private void InitializeEmptyEntryWithCabinetId()
        {
            _itsDataViewModel.CurrentEntry = _factory.CreateEntry();
            _itsDataViewModel.CurrentEntry.CabinetId = _memoryDataStore.RetrieveCabinetId();
        }

        public void Save()
        {
            _memoryDataStore.Persist(_itsDataViewModel.CurrentEntry);
        }
    }
}

